<?php
/**
 * 从数组或值对象中取值
 */
function gv($obj = null , $key = null){
    if(is_array($obj)){
        return  array_key_exists($key,$obj) ? $obj[$key] : null;
    }
    return null;
}

/**
 * get the system-configuration value
 */
function siteinfo($key){
    $keys = split('_',$key);
}

/**
 * 返回对应于MySQL中datetime的值
 *
 * @param <timestamp> $time 未指定时，将自动取当前timestamp
 * @return <string>
 */
function datetime($time){
    return date('Y-m-d H:i:s',(is_null($time))?time():$time);
}

/**
 * 返回定义于regexs.php里的表达式库
 * @return <array>
 */
function getJsRegexs(){
    $regexs = include COMMON_PATH.'regexs.php';
    $ret = array();
    foreach($regexs as $key=>$reg){
        $ret[$key] = substr($reg,1,strlen($reg)-2);
    }
    return $ret;
}

/**
 * 权限检查
 * @return boolean 有访问权限时返回true，否则返回false
 */
function testAccess($module,$action,$app=APP_NAME){
    $accessList = $_SESSION['_ACCESS_LIST_'];
    if(empty($accessList)){
        return false;
    }
    return !empty($accessList[strtoupper($app)][strtoupper($module)][strtoupper($action)]);
}

//clean javascript
function cleanJs($text){
    $text   =   trim($text);
    $text   =   stripslashes($text);
    $text   =   preg_replace('/<\?|\?>/','',$text);
    $text   =   preg_replace('/<script?.*\/script>/','',$text);
    $text   =   preg_replace('/<\/?(html|head|meta|link|base|body|title|style|script|form|iframe|frame|frameset)[^><]*>/i','',$text);
    while(preg_match('/(<[^><]+)(lang|onfinish|onmouse|onexit|onerror|onclick|onkey|onload|onchange|onfocus|onblur)[^><]+/i',$text,$mat)) $text=str_replace($mat[0],$mat[1],$text);
    while(preg_match('/(<[^><]+)(window\.|javascript:|js:|about:|file:|document\.|vbs:|cookie)([^><]*)/i',$text,$mat)) $text=str_replace($mat[0],$mat[1].$mat[3],$text);
    return $text;
}
//纯文本输出
function t($text){
    $text   =   h($text);
    $text   =   strip_tags($text);
    return $text;
}
//另一种纯文本输出
function t2($text){
    $text=strip_tags($text);
    return h($text);
}
/**
 * 摘要生成
 */
function summary($text,$lens=220){
    Load('extend'); //msubstr在扩展函数库中
    $txt = strip_tags($text);
    return msubstr(preg_replace('/(\t+)|(&nbsp;)+/','',str_replace('<br />','&nbsp;',h($txt))),0,$lens);
}